home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayBits.frm < prev    next >
Text File  |  1999-05-27  |  13KB  |  479 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayBits 
  4.    Caption         =   "PlayBits"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.TextBox txtNumFrames 
  15.       Height          =   285
  16.       Left            =   1560
  17.       TabIndex        =   10
  18.       Text            =   "100"
  19.       Top             =   120
  20.       Width           =   375
  21.    End
  22.    Begin VB.OptionButton optRunType 
  23.       Caption         =   "Looping"
  24.       Height          =   255
  25.       Index           =   2
  26.       Left            =   360
  27.       TabIndex        =   8
  28.       Top             =   1560
  29.       Width           =   1095
  30.    End
  31.    Begin VB.OptionButton optRunType 
  32.       Caption         =   "Reversing"
  33.       Height          =   255
  34.       Index           =   1
  35.       Left            =   360
  36.       TabIndex        =   7
  37.       Top             =   1200
  38.       Width           =   1095
  39.    End
  40.    Begin VB.OptionButton optRunType 
  41.       Caption         =   "One time"
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   360
  45.       TabIndex        =   6
  46.       Top             =   840
  47.       Value           =   -1  'True
  48.       Width           =   1095
  49.    End
  50.    Begin VB.TextBox txtFramesPerSecond 
  51.       Height          =   285
  52.       Left            =   1560
  53.       TabIndex        =   5
  54.       Text            =   "20"
  55.       Top             =   480
  56.       Width           =   375
  57.    End
  58.    Begin VB.PictureBox picFrame 
  59.       AutoRedraw      =   -1  'True
  60.       AutoSize        =   -1  'True
  61.       Height          =   375
  62.       Left            =   1560
  63.       ScaleHeight     =   21
  64.       ScaleMode       =   3  'Pixel
  65.       ScaleWidth      =   21
  66.       TabIndex        =   2
  67.       Top             =   1560
  68.       Visible         =   0   'False
  69.       Width           =   375
  70.    End
  71.    Begin VB.CommandButton cmdStart 
  72.       Caption         =   "Start"
  73.       Default         =   -1  'True
  74.       Enabled         =   0   'False
  75.       Height          =   375
  76.       Left            =   600
  77.       TabIndex        =   1
  78.       Top             =   2040
  79.       Width           =   855
  80.    End
  81.    Begin VB.PictureBox picCanvas 
  82.       Height          =   3810
  83.       Left            =   2040
  84.       ScaleHeight     =   250
  85.       ScaleMode       =   3  'Pixel
  86.       ScaleWidth      =   250
  87.       TabIndex        =   0
  88.       Top             =   0
  89.       Width           =   3810
  90.    End
  91.    Begin MSComDlg.CommonDialog dlgOpenFile 
  92.       Left            =   1560
  93.       Top             =   960
  94.       _ExtentX        =   847
  95.       _ExtentY        =   847
  96.       _Version        =   393216
  97.       CancelError     =   -1  'True
  98.    End
  99.    Begin VB.Label Label2 
  100.       Caption         =   "Frames to load:"
  101.       Height          =   255
  102.       Left            =   120
  103.       TabIndex        =   9
  104.       Top             =   120
  105.       Width           =   1455
  106.    End
  107.    Begin VB.Label Label1 
  108.       Caption         =   "Frames per second:"
  109.       Height          =   255
  110.       Index           =   1
  111.       Left            =   120
  112.       TabIndex        =   4
  113.       Top             =   480
  114.       Width           =   1455
  115.    End
  116.    Begin VB.Label lblResults 
  117.       Height          =   615
  118.       Left            =   120
  119.       TabIndex        =   3
  120.       Top             =   2640
  121.       Width           =   1815
  122.    End
  123.    Begin VB.Menu mnuFile 
  124.       Caption         =   "&File"
  125.       Begin VB.Menu mnuFileOpen 
  126.          Caption         =   "&Open..."
  127.          Shortcut        =   ^O
  128.       End
  129.       Begin VB.Menu mnuFileSaveSequence 
  130.          Caption         =   "&Save Sequence..."
  131.          Shortcut        =   ^S
  132.       End
  133.    End
  134. End
  135. Attribute VB_Name = "frmPlayBits"
  136. Attribute VB_GlobalNameSpace = False
  137. Attribute VB_Creatable = False
  138. Attribute VB_PredeclaredId = True
  139. Attribute VB_Exposed = False
  140. Option Explicit
  141.  
  142. Private NumImages As Integer
  143. Private MaxImage As Integer
  144. Private Playing As Boolean
  145.  
  146. Private Bytes() As Byte
  147. Private BytesPerImage As Long
  148.  
  149. Private NumPlayed As Long
  150.  
  151. ' Save all the images in a single file.
  152. Private Sub SaveSequence(file_name As String)
  153. Dim fnum As Integer
  154.  
  155.     ' Open the file.
  156.     fnum = FreeFile
  157.     Open file_name For Binary Access Write As #fnum
  158.  
  159.     ' Save the number of frames and the frame size.
  160.     Put #fnum, , NumImages
  161.     Put #fnum, , CSng(picCanvas.Width)
  162.     Put #fnum, , CSng(picCanvas.Height)
  163.     Put #fnum, , BytesPerImage
  164.  
  165.     ' Save the frames' bytes.
  166.     Put #fnum, , Bytes
  167.  
  168.     ' Close the file.
  169.     Close #fnum
  170. End Sub
  171. ' Load a sequence of images from a single file.
  172. Private Sub LoadSequence(file_name As String)
  173. Dim fnum As Integer
  174. Dim wid As Single
  175. Dim hgt As Single
  176.  
  177.     ' Open the file.
  178.     fnum = FreeFile
  179.     Open file_name For Binary Access Read As #fnum
  180.  
  181.     ' Get the number of frames and the frame size.
  182.     Get #fnum, , NumImages
  183.     Get #fnum, , wid
  184.     Get #fnum, , hgt
  185.     Get #fnum, , BytesPerImage
  186.  
  187.     ' Resize the display picture box.
  188.     picCanvas.AutoRedraw = False
  189.     picCanvas.Width = wid
  190.     picCanvas.Height = hgt
  191.     picCanvas.Picture = picCanvas.Image
  192.  
  193.     ' Get the frames' bytes.
  194.     MaxImage = NumImages - 1
  195.     ReDim Bytes(1 To BytesPerImage, 0 To MaxImage)
  196.     Get #fnum, , Bytes
  197.  
  198.     ' Close the file.
  199.     Close #fnum
  200.  
  201.     ' Display the first image.
  202.     SetBitmapBits picCanvas.Image, BytesPerImage, Bytes(1, 0)
  203.     picCanvas.Refresh
  204.     lblResults.Caption = ""
  205.     txtNumFrames.Text = Format$(NumImages)
  206. End Sub
  207.  
  208.  
  209. ' Load the images.
  210. Private Sub LoadImages(file_name As String)
  211. Dim base As String
  212. Dim i As Integer
  213. Dim bm As BITMAP
  214.  
  215.     ' Get the base file name.
  216.     base = Left$(file_name, Len(file_name) - 5)
  217.  
  218.     ' See how many frames the user wants to load.
  219.     If Not IsNumeric(txtNumFrames.Text) Then _
  220.         txtNumFrames.Text = Format$(10)
  221.     NumImages = CInt(txtNumFrames.Text)
  222.  
  223.     ' Get the first image.
  224.     picCanvas.AutoSize = True
  225.     picCanvas.Picture = LoadPicture(base & "0.bmp")
  226.  
  227.     ' See how big it is.
  228.     GetObject picCanvas.Image, Len(bm), bm
  229.     BytesPerImage = bm.bmWidthBytes * bm.bmHeight
  230.  
  231.     ' Make room for the bitmap bits.
  232.     MaxImage = NumImages - 1
  233.     ReDim Bytes(1 To BytesPerImage, 0 To MaxImage)
  234.  
  235.     ' Load the images.
  236.     On Error GoTo LoadPictureError
  237.     i = 0
  238.     Do While i < NumImages
  239.         lblResults.Caption = Format$(i + 1)
  240.         lblResults.Refresh
  241.  
  242.         ' Load the picture.
  243.         picFrame.Picture = LoadPicture(base & Format$(i) & ".bmp")
  244.  
  245.         ' Grab the image's bits.
  246.         GetBitmapBits picFrame.Image, BytesPerImage, Bytes(1, i)
  247.  
  248.         i = i + 1
  249.     Loop
  250.  
  251.     lblResults.Caption = ""
  252.     txtNumFrames.Text = Format$(NumImages)
  253.     Exit Sub
  254.  
  255. LoadPictureError:
  256.     ' We ran out of images early.
  257.     NumImages = i - 1
  258.     MaxImage = NumImages - 1
  259.     ReDim Preserve Bytes(1 To BytesPerImage, 0 To MaxImage)
  260.     Resume Next
  261. End Sub
  262.  
  263. ' Run the animation until Playing is false.
  264. Private Sub PlayImages()
  265. Dim ms_per_frame As Integer
  266. Dim start_time As Long
  267. Dim stop_time As Long
  268.  
  269.     ' See how long it should be between frames.
  270.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  271.         txtFramesPerSecond.Text = "20"
  272.     ms_per_frame = 1000 / CInt(txtFramesPerSecond.Text)
  273.  
  274.     ' Start the appropriate animation.
  275.     NumPlayed = 0
  276.     start_time = GetTickCount
  277.     If optRunType(0).value Then
  278.         PlayImagesOnce ms_per_frame
  279.     ElseIf optRunType(1).value Then
  280.         PlayImagesBackAndForth ms_per_frame
  281.     Else
  282.         PlayImagesLooping ms_per_frame
  283.     End If
  284.  
  285.     ' Display results.
  286.     stop_time = GetTickCount
  287.     lblResults.Caption = _
  288.         Format$(NumPlayed) & " frames/" & _
  289.         Format$((stop_time - start_time) / 1000#, "0.00") & _
  290.         " sec" & vbCrLf & vbCrLf & _
  291.         Format$(CSng(NumPlayed) / ((stop_time - start_time) / 1000#), "0.00") & _
  292.         " frames/sec"
  293. End Sub
  294. ' Run the animation until Playing is false.
  295. Private Sub PlayImagesLooping(ByVal ms_per_frame As Integer)
  296.     ' Start the animation.
  297.     Do While Playing
  298.         PlayImagesOnce ms_per_frame
  299.     Loop
  300. End Sub
  301. ' Run the animation once or until Playing is False.
  302. Private Sub PlayImagesOnce(ByVal ms_per_frame As Integer)
  303. Dim i As Integer
  304. Dim next_time As Long
  305.  
  306.     ' Get the current time.
  307.     next_time = GetTickCount
  308.  
  309.     ' Start the animation.
  310.     For i = 0 To NumImages - 1
  311.         ' Display the next frame.
  312.         SetBitmapBits picCanvas.Image, BytesPerImage, Bytes(1, i)
  313.         picCanvas.Refresh
  314.         NumPlayed = NumPlayed + 1
  315.  
  316.         ' Wait till we should display the next frame.
  317.         next_time = next_time + ms_per_frame
  318.         WaitTill next_time
  319.  
  320.         If Not Playing Then Exit For
  321.     Next i
  322. End Sub
  323. ' Run the animation reversed once or until Playing
  324. ' is False.
  325. Private Sub PlayImagesReversed(ByVal ms_per_frame As Integer)
  326. Dim i As Integer
  327. Dim next_time As Long
  328.  
  329.     ' Get the current time.
  330.     next_time = GetTickCount
  331.  
  332.     ' Start the animation.
  333.     For i = NumImages - 1 To 0 Step -1
  334.         ' Display the next frame.
  335.         SetBitmapBits picCanvas.Image, BytesPerImage, Bytes(1, i)
  336.         picCanvas.Refresh
  337.         NumPlayed = NumPlayed + 1
  338.  
  339.         ' Wait till we should display the next frame.
  340.         next_time = next_time + ms_per_frame
  341.         WaitTill next_time
  342.  
  343.         If Not Playing Then Exit For
  344.     Next i
  345. End Sub
  346. ' Run the animation forward and backward until
  347. ' Playing is False.
  348. Private Sub PlayImagesBackAndForth(ByVal ms_per_frame As Integer)
  349.     ' Start the animation.
  350.     Do While Playing
  351.         PlayImagesOnce ms_per_frame
  352.         If Not Playing Then Exit Do
  353.         PlayImagesReversed ms_per_frame
  354.     Loop
  355. End Sub
  356.  
  357. ' Start or stop playing.
  358. Private Sub CmdStart_Click()
  359.     If Playing Then
  360.         Playing = False
  361.         cmdStart.Caption = "Stopped"
  362.         cmdStart.Enabled = False
  363.     Else
  364.         cmdStart.Caption = "Stop"
  365.         lblResults.Caption = ""
  366.         DoEvents
  367.         Playing = True
  368.         PlayImages
  369.         Playing = False
  370.         cmdStart.Caption = "Start"
  371.         cmdStart.Enabled = True
  372.     End If
  373. End Sub
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380. Private Sub Form_Load()
  381.     dlgOpenFile.InitDir = App.Path
  382. End Sub
  383.  
  384. ' Load new image files.
  385. Private Sub mnuFileOpen_Click()
  386. Dim file_name As String
  387.  
  388.     ' Let the user select a file.
  389.     On Error Resume Next
  390.     dlgOpenFile.Filter = _
  391.         "Start Files (*_0.bmp)|*_0.bmp|" & _
  392.         "Sequence Files (*.seq)|*.seq|" & _
  393.         "Bitmap Files (*.bmp)|*.bmp|" & _
  394.         "All Files (*.*)|*.*"
  395.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  396.     dlgOpenFile.ShowOpen
  397.     If Err.Number = cdlCancel Then
  398.         Exit Sub
  399.     ElseIf Err.Number <> 0 Then
  400.         Beep
  401.         MsgBox "Error selecting file.", , vbExclamation
  402.         Exit Sub
  403.     End If
  404.     On Error GoTo 0
  405.  
  406.     Screen.MousePointer = vbHourglass
  407.     DoEvents
  408.  
  409.     file_name = Trim$(dlgOpenFile.FileName)
  410.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  411.         - Len(dlgOpenFile.FileTitle) - 1)
  412.     Caption = "PlayWait [" & dlgOpenFile.FileTitle & "]"
  413.  
  414.     ' Load the pictures.
  415.     On Error GoTo LoadError
  416.     If LCase$(Right$(file_name, 4)) = ".seq" Then
  417.         LoadSequence file_name
  418.     Else
  419.         LoadImages file_name
  420.     End If
  421.     On Error GoTo 0
  422.  
  423.     cmdStart.Enabled = True
  424.     mnuFileSaveSequence.Enabled = True
  425.     Screen.MousePointer = vbDefault
  426.     Exit Sub
  427.  
  428. LoadError:
  429.     Screen.MousePointer = vbDefault
  430.     MsgBox "Error " & Format$(Err.Number) & _
  431.         " opening file '" & file_name & "'" & vbCrLf & _
  432.         Err.Description
  433. End Sub
  434.  
  435. ' Save the images in a sequence file.
  436. Private Sub mnuFileSaveSequence_Click()
  437. Dim file_name As String
  438.  
  439.     ' Let the user select a file.
  440.     On Error Resume Next
  441.     dlgOpenFile.Filter = _
  442.         "Sequence Files (*.seq)|*.seq|" & _
  443.         "All Files (*.*)|*.*"
  444.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  445.     dlgOpenFile.ShowSave
  446.     If Err.Number = cdlCancel Then
  447.         Exit Sub
  448.     ElseIf Err.Number <> 0 Then
  449.         Beep
  450.         MsgBox "Error selecting file.", , vbExclamation
  451.         Exit Sub
  452.     End If
  453.     On Error GoTo 0
  454.  
  455.     Screen.MousePointer = vbHourglass
  456.     DoEvents
  457.  
  458.     file_name = Trim$(dlgOpenFile.FileName)
  459.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  460.         - Len(dlgOpenFile.FileTitle) - 1)
  461.     Caption = "PlayWait [" & dlgOpenFile.FileTitle & "]"
  462.  
  463.     ' Save oad the pictures.
  464.     On Error GoTo SaveError
  465.     SaveSequence file_name
  466.     On Error GoTo 0
  467.  
  468.     Screen.MousePointer = vbDefault
  469.     Exit Sub
  470.  
  471. SaveError:
  472.     Screen.MousePointer = vbDefault
  473.     MsgBox "Error " & Format$(Err.Number) & _
  474.         " opening file '" & file_name & "'" & vbCrLf & _
  475.         Err.Description
  476. End Sub
  477.  
  478.  
  479.